package ConnectionsDatebases;

import org.dom4j.Document;
import org.dom4j.io.SAXReader;
import org.junit.Test;
import sun.security.util.Password;

import java.sql.*;

public class Dbutils {
    private static String driverClassName = "com.mysql.jdbc.Driver";
    private static String user;
    private static String password ;
    private static String port;
    private static String host;
    private static String databases;
    static {
            SAXReader saxReader = new SAXReader();
            try {
                Document document = saxReader.read("conf/database.xml");
                 host = document.selectSingleNode("/properties/property[@current='1']/host/text()").getStringValue();
                 password = document.selectSingleNode("/properties/property[@current='1']/password/text()").getStringValue();
                 port = document.selectSingleNode("/properties/property[@current='1']/port/text()").getStringValue();
                 user = document.selectSingleNode("/properties/property[@current='1']/username/text()").getStringValue();
                 databases = document.selectSingleNode("/properties/property[@current='1']/database/text()").getStringValue();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    public static Connection Connect() {
        Connection connect = null;
        try {
            Class.forName(driverClassName);
        } catch (ClassNotFoundException e) {
            System.out.println("类加载失败");
            e.printStackTrace();
        }
        try {
            String url = "jdbc:mysql://"+host+":"+port+"/"+databases+"?characterEncoding=utf-8";
            connect = DriverManager.getConnection(url, user, password);
        } catch (SQLException e) {
            System.out.println("数据库连接失败");
            e.printStackTrace();
        }
        return connect;
    }
    public static Statement getStatement(Connection con){
        Statement statement = null;
        try {
             statement = con.createStatement();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return statement;
    }
    public static PreparedStatement getPrepareStatement(Connection connection, String sql) {
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return preparedStatement;
    }

    public static void closeConnection(Connection con) {
        if (con != null) {
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                con = null;
            }
        }
    }

    public static void closePreparestatement(PreparedStatement preparedStatement) {
        if (preparedStatement != null) {
            try {
                preparedStatement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                preparedStatement = null;
            }
        }
    }
    public static void closeStatement(Statement stmt){
        if(stmt!=null){
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                stmt=null;
            }
        }
    }
}
